package com.cstam.web;

import javax.servlet.http.HttpSession;

import com.cstam.domain.User;
import com.cstam.service.UserManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/test")
public class TestController {
	
	/************************************************************
	 * @Autowired
	 ************************************************************/
	// Autowired on Setter
	/*private UserManager userManager;
	
	@Autowired
	public void serUserManager(UserManager userManager){
		this.userManager = userManager;
	}*/
	
	// Autowired on Properties
	@Autowired
	//@Qualifier("userManager")
	private UserManager userManager;
	
	// Autowired on Constructor
	/*private UserManager userManager;
	
	@Autowired
	public TestController(UserManager userManager){
		this.userManager = userManager;
	}*/
	
	/************************************************************
	 * @RequestMapping
	 ************************************************************/
	@RequestMapping(method=RequestMethod.GET)
	public String welcome(ModelMap model) {
 
		model.addAttribute("message", "ModelMap");
		return "index";
 
	}
	
	@RequestMapping(value="/{value}")
	public String welcomeWithValue(ModelMap model, @PathVariable String value) {
 
		model.addAttribute("message", "ModelMapWithValue");
		model.addAttribute("value", value);
		return "index";
 
	}
	
	@RequestMapping("/mv/")
	public ModelAndView welcome() {
		
		ModelAndView ret = new ModelAndView("index");
		ret.addObject("message", "ModelAndView");
		return ret;
 
	}
	
	@RequestMapping(value="/mv/{value}", method=RequestMethod.GET)
	public ModelAndView welcomeWithValue(@PathVariable("value") String valueString) {
		
		ModelAndView ret = new ModelAndView("index");
		ret.addObject("message", "ModelAndViewWithValue");
		ret.addObject("value", valueString);
		return ret;
 
	}
	
	/************************************************************
	 * Test User
	 ************************************************************/
	@RequestMapping("/users")
	public ModelAndView showUser() {
		ModelAndView ret = new ModelAndView("usertest");
		ret.addObject("users", userManager.getAllUsers());
		
		return ret;
	}
	
	@RequestMapping("/users/nickname/{nickname}")
	public ModelAndView showUser(@PathVariable String nickname) {
		ModelAndView ret = new ModelAndView("usertest");
		ret.addObject("user", userManager.getUserByUsername(nickname));
		
		return ret; 
	}
	
	@RequestMapping("/users/id/{iduser}")
	public ModelAndView showUser(@PathVariable int iduser) {
		ModelAndView ret = new ModelAndView("usertest");
		ret.addObject("user", userManager.getUserByIduser(iduser));
		
		return ret; 
	}
	
	@RequestMapping(value="/users/create", method=RequestMethod.POST)
	public ModelAndView createUser(@RequestParam String nickname, @RequestParam String password, @RequestParam int type) {
		ModelAndView ret = new ModelAndView("exp/success");
		if(userManager.register(nickname, password) == 1){
			ret.addObject("msg", "Success!");
			ret.addObject("alert", "1");
		}else{
			ret.addObject("msg", "Fail!");
			ret.addObject("alert", "1");
		}
		
		return ret; 
	}
	
	/*public ModelAndView createUser(@ModelAttribute("user") User user) {
		ModelAndView ret = new ModelAndView("exp/success");
		if(userManager.creatUser(user.getNickname(), user.getPassword(), user.getType()) == 1){
			ret.addObject("msg", "Success!");
			ret.addObject("alert", "1");
		}else{
			ret.addObject("msg", "Fail!");
			ret.addObject("alert", "1");
		}
		
		return ret; 
	}*/
}
